package com.hkts.naturalgas.organizational.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.hkts.naturalgas.feign.FeignInterface;
import com.hkts.naturalgas.organizational.entity.HontPost;
import com.hkts.naturalgas.organizational.entity.HontUser;
import com.hkts.naturalgas.organizational.entity.HontUserPost;
import com.hkts.naturalgas.organizational.mapper.HontPostMapper;
import com.hkts.naturalgas.organizational.mapper.HontUserMapper;
import com.hkts.naturalgas.organizational.mapper.HontUserPostMapper;
import com.hkts.naturalgas.organizational.orgVo.RoleIdsAndUserOrgCodeVo;
import com.hkts.naturalgas.organizational.orgVo.UserNameAndPostTitleVo;
import com.hkts.naturalgas.organizational.service.IHontUserService;
import com.hkts.naturalgas.performance.performanceVo.HontPostTitleVo;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2020-12-23
 */
@Service
@Transactional
public class HontUserServiceImpl extends ServiceImpl<HontUserMapper, HontUser> implements IHontUserService {


    @Autowired
    private HontUserMapper hontUserMapper;
    @Autowired
    private HontUserPostMapper hontUserPostMapper;
    @Autowired
    private FeignInterface feignInterface;

    @Override
    public IPage<HontUser> selectPage(Page<HontUser> page, HontUser hontUser) {
        QueryWrapper<HontUser> query = Wrappers.query();
        QueryWrapper<HontUser> entityWrapper = getEntityWrapper(query, hontUser);
        return hontUserMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HontUser> selectList(HontUser hontUser) {
        QueryWrapper<HontUser> query = Wrappers.query();
        QueryWrapper<HontUser> entityWrapper = getEntityWrapper(query, hontUser);
        return hontUserMapper.selectList(entityWrapper);
    }
    /*
    * 根据用户id 查询 用户姓名及postTitle
    * */

    @Override
    public UserNameAndPostTitleVo getHontUserNameAndPostTitle(HontUser hontUser) {
        HontUser hontUser1 = hontUserMapper.getHontUserById(hontUser);
        UserNameAndPostTitleVo vo=new UserNameAndPostTitleVo();
        vo.setUserName(hontUser1.getNickName());
        List<HontPostTitleVo> byUserId = hontUserPostMapper.getByUserId(hontUser.getId());
        if (byUserId.size() > 0){
            String[] strings = new String[byUserId.size()];
            for (int i = 0; i < byUserId.size(); i++) {
                strings[i] = byUserId.get(i).getTitle();
            }
            vo.setPostTitle(strings);
        }else {
            vo.setPostTitle(new String[0]);
        }

        return vo;
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HontUser> getEntityWrapper(QueryWrapper<HontUser> entityWrapper, HontUser hontUser) {
        //条件拼接
        if (StringUtils.isNotBlank(hontUser.getAddress())) {
            entityWrapper.like(HontUser.ADDRESS, hontUser.getAddress());
        }
        if (StringUtils.isNotBlank(hontUser.getAvatar())) {
            entityWrapper.like(HontUser.AVATAR, hontUser.getAvatar());
        }
        if (StringUtils.isNotBlank(hontUser.getDescription())) {
            entityWrapper.like(HontUser.DESCRIPTION, hontUser.getDescription());
        }
        if (StringUtils.isNotBlank(hontUser.getEmail())) {
            entityWrapper.like(HontUser.EMAIL, hontUser.getEmail());
        }
        if (StringUtils.isNotBlank(hontUser.getMobile())) {
            entityWrapper.like(HontUser.MOBILE, hontUser.getMobile());
        }
        if (StringUtils.isNotBlank(hontUser.getNickName())) {
            entityWrapper.like(HontUser.NICK_NAME, hontUser.getNickName());
        }
        if (StringUtils.isNotBlank(hontUser.getPassStrength())) {
            entityWrapper.like(HontUser.PASS_STRENGTH, hontUser.getPassStrength());
        }
        if (StringUtils.isNotBlank(hontUser.getPassWord())) {
            entityWrapper.like(HontUser.PASS_WORD, hontUser.getPassWord());
        }
        if (StringUtils.isNotBlank(hontUser.getSex())) {
            entityWrapper.like(HontUser.SEX, hontUser.getSex());
        }
        if (StringUtils.isNotBlank(hontUser.getStatus())) {
            entityWrapper.like(HontUser.STATUS, hontUser.getStatus());
        }
        if (StringUtils.isNotBlank(hontUser.getStreet())) {
            entityWrapper.like(HontUser.STREET, hontUser.getStreet());
        }
               /* if(StringUtils.isNotBlank(hontUser.getType())){
                entityWrapper.like(HontUser.TYPE,hontUser.getType());
                }
                if(StringUtils.isNotBlank(hontUser.getUserLevel())){
                entityWrapper.like(HontUser.USER_LEVEL,hontUser.getUserLevel());
                }*/
        if (StringUtils.isNotBlank(hontUser.getUserName())) {
            entityWrapper.like(HontUser.USER_NAME, hontUser.getUserName());
        }
        if (StringUtils.isNotBlank(hontUser.getDepartmentId())) {
            entityWrapper.like(HontUser.DEPARTMENT_ID, hontUser.getDepartmentId());
        }
        if (StringUtils.isNotBlank(hontUser.getCreateBy())) {
            entityWrapper.like(HontUser.CREATE_BY, hontUser.getCreateBy());
        }
                /*if(StringUtils.isNotBlank(hontUser.getCreateTime())){
                entityWrapper.like(HontUser.CREATE_TIME,hontUser.getCreateTime());
                }
                if(StringUtils.isNotBlank(hontUser.getDelFlag())){
                entityWrapper.like(HontUser.DEL_FLAG,hontUser.getDelFlag());
                }*/
        if (StringUtils.isNotBlank(hontUser.getUpdateBy())) {
            entityWrapper.like(HontUser.UPDATE_BY, hontUser.getUpdateBy());
        }
             /*   if(StringUtils.isNotBlank(hontUser.getUpdateTime())){
                entityWrapper.like(HontUser.UPDATE_TIME,hontUser.getUpdateTime());
                }
                if(StringUtils.isNotBlank(hontUser.getApprove())){
                entityWrapper.like(HontUser.APPROVE,hontUser.getApprove());
                }*/
        if (StringUtils.isNotBlank(hontUser.getPostId())) {
            entityWrapper.like(HontUser.POST_ID, hontUser.getPostId());
        }
        if (StringUtils.isNotBlank(hontUser.getDepartmentCopyId())) {
            entityWrapper.like(HontUser.DEPARTMENT_COPY_ID, hontUser.getDepartmentCopyId());
        }
              /*  if(StringUtils.isNotBlank(hontUser.getUpdateLastTime())){
                entityWrapper.like(HontUser.UPDATE_LAST_TIME,hontUser.getUpdateLastTime());
                }*/
        return entityWrapper;
    }

    /*
    * 获取任务的接受者并远程调用发消息接口
    * */

    @Override
    public void getMessageRecever(String params) {
        String[] split = params.split(",");
        String[] split1 = split[0].split(":");
        RoleIdsAndUserOrgCodeVo vo=new RoleIdsAndUserOrgCodeVo();
        vo.setRoleIds(split1);
        vo.setUserOrgCode(split[1]);
        List<String> list= hontUserMapper.getMessageRecever(vo);
        String[] openIds=new String[list.size()];
        for(int i=0;i<list.size();i++){
            openIds[i]=list.get(i);
        }
        feignInterface.send(openIds);
        System.out.println("list:"+list.toString());
    }
}
